﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using Unity.Errors.types;

namespace DbGenerator.Utilities
{
    public static class SReflectionUtilities
    {
        public static object SelectProperty(Type pType, string propertyName)
        {
            //Construct instance:
            ConstructorInfo constructor = pType.GetConstructor(System.Type.EmptyTypes);
            object instance = constructor.Invoke(null);

            //Collect properties:
            PropertyInfo[] piArr = pType.GetProperties();

            //Find the property
            int errorCount = -1;
            int limit = piArr.Length;
            object propertyValue = null;
            for (int i = 0; i < limit; i++)
            {
                if (piArr[i].Name == propertyName)
                {
                    errorCount++;
                    propertyValue = piArr[i].GetValue(instance, null);
                }
            }
            SErrorManager.Errors("ShouldBeSingle")
                .Assert(errorCount == 0
                , new TErrorInfo(typeof(SReflectionUtilities), "SelectProperty")
                , "propertyValue", errorCount);
            return propertyValue;
        }
    }
}
